package com.zzyl.nursing.service.impl;

import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.nursing.dto.ReservationDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.ReservationMapper;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.service.IReservationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;

/**
 * 预约信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-31
 */
@Service
public class ReservationServiceImpl extends ServiceImpl<ReservationMapper, Reservation> implements IReservationService
{
    @Autowired
    private ReservationMapper reservationMapper;

    /**
     * 取消预约信息
     *
     * @param id
     */
    @Override
    public void cancelReservation(Long id) {
        //根据id查询出预约信息
        Reservation reservation = this.getOne(Wrappers.<Reservation>lambdaQuery()
                .eq(Reservation::getId, id));
        reservation.setStatus(2);
    }

    /**
     * 新增预约信息
     *
     * @param reservationDto
     */
    @Override
    public void insertReservation(ReservationDto reservationDto) {
        //1.首先查询当前取消预约数量
        int cancelledReservationCount = this.getCancelledReservationCount();
        if (cancelledReservationCount >= 3) {
            //取消预约数量达到3个，则不能再预约
            throw new RuntimeException("取消预约数量达到10个，不能再预约");
        }
        //2.新增预约信息
        Reservation reservation = BeanUtil.toBean(reservationDto, Reservation.class);
        reservation.setStatus(0);
        reservationMapper.insert(reservation);
    }

    /**
     * 查询取消预约的数量
     */
    @Override
    public int getCancelledReservationCount() {
        return Math.toIntExact(this.count(new LambdaQueryWrapper<Reservation>()
                .eq(Reservation::getStatus, "2")));
    }

    /**
     * 查询预约信息列表
     * 
     * @param reservation 预约信息
     * @return 预约信息
     */
    @Override
    public List<Reservation> selectReservationList(Reservation reservation)
    {
        return reservationMapper.selectReservationList(reservation);
    }

}
